<?php

class TaskAction extends Action
{    

	/**
	 * TaskAction::_initialize()
	 * 
	 * @return
	 */
	public function _initialize()
    {
        checkLoginSession();
    }
	
	/**
	 * TaskAction::tasking()
	 * 
	 * @return
	 */
	public function tasking()
	{
	$taskDao = D("Task");
	$tname = 'Task';
	$Field = 'id_gezi_task';
	$Asc = ' ASC';
	
	$searchwords = '';
	$arrayVa = ShowTable($tname, $Field, $Asc, $searchwords);
	
	$dbList = $arrayVa['a'];
	$page = $arrayVa['b'];
	if (count($arrayVa['a']) < 1 or ($arrayVa['a'] == null))
	{
	$this->assign('empty', '空记录');
	
	}
	$this->assign('dbList', $dbList);
	$this->assign('page', $page);
	$this->display('Task:index');	
	}
    
    function showTask()
    {
     $status =$_GET['status'];
     $type =isset($_GET['type'])?$_GET['type']:NULL;
     
     $TaskList1 = getTaskList($status,$type); 
     //$TaskList1 = $rtArrayT['a'];$pageT =$rtArrayT['b'];
     $this->assign('TaskList1',$TaskList1);     
     $this->assign('status',$status);
     
     $this->display('Task:taskList');       
    }
    
    
 public function  addApplytask()
 {
   //ajax提交后台1.insert taskexec表一条记录，update team表的now_capcity,提交一条申请
   $id_task = $_POST['id_gezi_task'];
   $taskpage = $_POST['taskpage'];
   $id_team = $_POST['id_team'];
    $score = $_POST['score'];   
    $data['id_task'] = $_POST['id_gezi_task'];
    $data['bTime'] = date('Y-m-d H-i-s');
    $data['id_team'] = $_POST['id_team'];
    $data['taskPageNum'] = $taskpage;    
     $data['score'] = $score;    

    $Task_exc = M('Task_exc');
    $topid = $Task_exc->add($data);
    
    $Team = M('Team');
    $effect = $Team->setDec('now_capcity'," `id_gezi_team`=".$id_team,$taskpage);
    
    
    //查询exc表，如果task对应的记录有6条了，则进入下一步
    $recsexc =$Task_exc->where('id_task = '.$id_task)->order('score')->findAll();
    $cnt = count($recsexc);
    if($cnt==6)//够6个团队申请了
    {
     $sTeamid =  $recsexc[5]['id_team'];    
     //获得分数最高的团队，发消息给此团队所有成员，让他们提交自己承诺分发的张数，不超过10天内的能力
     $Team_member = M('Team_member');
     $recsm = $Team_member->where('id_gezi_team='.$sTeamid)->findAll();
     $cnmt = count($recsm);     
     $msg = "<a href=".'"'."javascript:readMe("."'receive'".",'shareTask'".')"'.">请速领取团队任务！</a> ";
     for($i=0;$i<$cnmt;$i++)//群发给分最高的团队所有成员消息
     {
        $topid1 = addMsg($msg,'System',$recsm[$i]['id_user'],'getTaskCount','bs','id_team:'.$id_team,'id_task:'.$id_task,'taskpage:'.$taskpage);        
     }
    }
    
    
    if($topid and  $effect )
    {
        echo ajaxrt('ok','Apply Send to System');
    }
    else
    {
        echo ajaxrt('error','System Data Operation Error');
    }
 }
 
 //任务团队成员认领各自的数量,ajax调用
 public function getTaskCount(){
   $my_task_count = $_POST['count'];//认领的数量   
   $id_user = $_SESSION['UserInfo']['id'];
   $person_now_capcity =  getPersonNcacity($id_user);   
   $id_team = $_POST['id_team'];
   $id_task = $_POST['id_task'];
   $rt3 = checkTaskMG($id_task,$my_task_count);//更新任务可认领张数,检查任务张数是否已经被认领完毕,完毕后进入印刷
   if( ($my_task_count<=$person_now_capcity) and $rt3 )//能力范围内
   {
    $rt = setPersonNCapcity('dec',$my_task_count,$id_user);
    $rt2 = addTask_mem($id_user,$id_team,$id_task,$my_task_count);
   
    if($rt and $rt2)
    {echo ajaxrt('ok','Apply Task Count OK');}
    else
    {echo ajaxrt('error','System Data Operation Error!');}
   } 
   else//超出能力范围
   {
    echo ajaxrt('error','Overload your capcity! lower your capcity is now：'.$person_now_capcity);
   }
 }

//为认领任务份额做准备数据
    public function preShareTask(){
    $id_task = $_GET['id_task'];
    $taskpage = $_GET['taskpage'];
    $id_user = $_SESSION['UserInfo']['id'];
    $person_now_capcity = getPersonNcacity($id_user);
    $task_now_page = getTasknPage($id_task);
    
    $rtis = isExistTaskMember($id_user,$id_task);

    $this->assign('id_task',$id_task);
    $this->assign('taskpage',$taskpage);
    $this->assign('person_now_capcity',$person_now_capcity);
    $this->assign('task_now_page',$task_now_page);
    $this->assign('isexistMember',$rtis);    
    
    $this->display('Public:shareTask');    
    }
   
   //当前只可创建团队任务、团队交换任务 
   public function createTask()
   {
      $this->display('Task:createTask'); 
    
   }
  
  //客户端提交数据，创建团队任务/团队交换任务
   public function addTeamTask()
   {
    $type =$_POST['type'];//团队任务或者团队交换任务
    $taskpage =$_POST['taskpage'];
    $geziPp =$_POST['geziPp'];//个人交换各自
    $mypage =$_POST['mypage'];//个人认领的投放数量
    $geziX =$_POST['geziX'];//格子系数
    $geziG =$_POST['geziG'];//格子工资
    $paySec =$_POST['paySec'];//支付密语
    $id_team = $_SESSION['UserInfo']['team_id'];
    $id_user = $_SESSION['UserInfo']['id'];
    $needGezi = ($geziPp * $taskpage) -$geziG;
    $tr = checkMoneyEnough($needGezi);
    
    $prt = checkPaySec($paySec);

    if($tr and $prt)//有钱则扣除格子
    {
        $trtr = jianqian($id_user,$needGezi);
        $describe = "支付交换任务所需格子";
        $typeTrade =  "PayExchageGezi";
        $yty = geziTradeLog($id_user,$needGezi,$describe,$typeTrade); 

        if(!($trtr and $yty ) )
        {
        echo ajaxrt('error',"支付错误");        
        exit();      
        }
    }
    else
    {
    echo ajaxrt('error',"余额不足或者支付密语错误,需要格子:".$needGezi."，请检查或充值");
    exit();        
    }
    //先查询个人投放能力是否满足承诺能力，团队投放能力是否满足任务总数
    //客户端完成
    $tnc = getTeamNcacity($id_team);
    $pnc = getPersonNcacity($id_user);
    if( ($tnc<$taskpage) OR ($pnc<$mypage))//当前只允许团队队长创建团队投放能力以内的投放任务张数
    {
        ECHO ajaxrt('error','Overload! '.$tnc.'<'.$taskpage.' OR '.$pnc.'<'.$mypage);
    }
    else
    {
            // 发消息给团队成员消息,点击填写认领任务张数
        $msg="<a href=".'"'."javascript:readMe("."'receive'".",'shareTeamTask'".')"'.">请速领取团队任务！</a> ";
        $rt = sendTeamMsg($id_team,$msg,'System','ShareTeamTask','bs');
    
        
        ///建立任务rec
        $rt1 = createTaskRec($type,$taskpage,$mypage,$geziPp,$id_team);
    
        //扣减队长的投放能力
        $rt2 = setPersonNCapcity('dec',$mypage,$_SESSION['UserInfo']['id']);
    
        //扣减团队投放能力
        $rt4 = setTeamNCaocity('dec',$taskpage,$_SESSION['UserInfo']['team_id']);
    
        //把团长加入task_member表记录
        $rt3 = addTask_mem($_SESSION['UserInfo']['id'],$_SESSION['UserInfo']['team_id'],$rt1,$mypage,$geziPp);
        
        //出钱了，交换格子了，要加入prj，taskprj，prj_pdt 记录
        
        $owner = $_SESSION['UserInfo']['name'];
        $rt5 = addPrjrec($geziPp,$taskpage,$owner,$id_user,$type,'Adding');
        
        $rt6 = addTaskprjRec($rt1,$rt5);
        
        $rt7 = addPrjpdtRec($rt5,$geziPp);
        
        if($rt and $rt1 and  $rt2 and $rt3 and $rt4 and  $rt5 and  $rt6 and  $rt7  )
        {
            
            echo ajaxrt('ok','Create  '.$type.' Task OK');
        }
        else
        {
            echo ajaxrt('error','AddTeamTask Error');
        }            
    }
   }  
   

}
?>